package com.noble.meether.chat.ui.imui.util;

import android.annotation.SuppressLint;

import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.meether.chat.msgtype.base.BaseMessage;
import com.noble.meether.chat.unread.UnreadMgr;
import com.noble.meether.fragment.mail.bean.UserMessage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * list排序
 */
public class SortList {

    // =============================== Mail信箱排序 ===================================

    /**
     * 根据未读消息数进行列表排序
     */
    public static void reSortList(List<UserMessage> list) {
        if (list == null || list.size() <= 0) return;
        initUnread(list);
        Collections.sort(list, (userLight, t1) -> {
            // 降序排列
            if (userLight.getMsgUnreadNum() > t1.getMsgUnreadNum()) {
                return -1;
            }
            if (userLight.getMsgUnreadNum() == t1.getMsgUnreadNum()) {
                return 0;
            }
            return 1;
        });
    }

    private static void initUnread(List<UserMessage> list) {
        UnreadMgr unreadMgr = ModuleMgr.getUnreadMgr();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setMsgUnreadNum(unreadMgr.getMsgKeyNum(list.get(i).getWhisperID()));
        }
    }

    /**
     * 去重： 根据uid
     */
    public static List<UserMessage> removeDuplicateOrder(List<UserMessage> orderList) {
        Set<UserMessage> set = new TreeSet<>((a, b) -> {
            // 字符串则按照asicc码升序排列
            return a.getSWhisperID().compareTo(b.getSWhisperID());
        });
        set.addAll(orderList);
        return new ArrayList<>(set);
    }


    // =============================== 排序 ===================================
    @SuppressLint("UseValueOf")
    public static void sortListView(List<BaseMessage> baseMessages) {
        if (baseMessages == null || baseMessages.size() < 2) {
            return;
        }

        Collections.sort(baseMessages, (arg0, arg1) -> new Long(arg0.getId()).compareTo(arg1.getId()));
    }

    // 时间排序
    @SuppressLint("UseValueOf")
    public static void sortTimeListView(List<BaseMessage> baseMessages) {
        if (baseMessages == null || baseMessages.size() < 2) {
            return;
        }

        Collections.sort(baseMessages, (arg0, arg1) -> new Long(arg0.getTime()).compareTo(arg1.getTime()));
    }

    /**
     * 权重+时间排序 消息列表专用
     *
     * @param baseMessages
     */
    public static void sortWeightTimeListView(List<BaseMessage> baseMessages) {
        if (baseMessages == null || baseMessages.size() < 2) {
            return;
        }

        Collections.sort(baseMessages, (arg0, arg1) -> {
            int i = Integer.valueOf(arg1.getWeight()).compareTo(arg0.getWeight());
            if (i == 0) {
                return (arg1.getTime() + "").compareTo(arg0.getTime() + "");
            } else {
                return i;
            }
        });
    }
}
